package com.example.ldachu.testdownload;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.ldachu.testdownload.http.RetrofitFactory;
import com.example.ldachu.testdownload.http.base.BaseObserver;
import com.example.ldachu.testdownload.http.base.BaseResponse;
import com.example.ldachu.testdownload.http.bean.IpAddressResponse;
import com.example.ldachu.testdownload.http.bean.VerifyAppVersion;
import com.example.ldachu.testdownload.http.util.RxUtil;
import com.example.ldachu.testdownload.http.util.ToastUtil;

import java.io.File;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv;
    private TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        tv = (TextView) findViewById(R.id.tv);
        tv1 = (TextView) findViewById(R.id.tv1);

        tv.setOnClickListener(this);
        tv1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv:
                downloadPic();
                break;

            case R.id.tv1:

//                Toast.makeText(this, "IP", Toast.LENGTH_SHORT).show();
                //获取IP地址
                getIpAddress();
                break;
        }
    }

    /**
     * 下载图片
     */
    private void downloadPic() {
        String url = "https://img251.pinhui001.cn/group1/M00/00/E3/CqArGlo7Jw-ADByBABARVdQOkZ0427.png";


        RetrofitFactory.getInstance().getService()
                .getVerifyAppVersion("dd")
                .compose(RxUtil.<BaseResponse<VerifyAppVersion>>schedulers())
                .subscribe(new BaseObserver<VerifyAppVersion>(this) {
                    @Override
                    public void onSuccess(BaseResponse<VerifyAppVersion> response) {

                    }
                });


        //图片下载
        RetrofitFactory.getInstance().getService()
                .downloadPic(url)
                .compose(RxUtil.schedulers())
                .map(new Function<ResponseBody,Bitmap>(){

                    @Override
                    public Bitmap apply(ResponseBody responseBody) throws Exception {
                        if(saveToSdcard(responseBody)){
                            Bitmap bitmap = BitmapFactory.decodeFile(getExternalFilesDir(null) + File.separator + "baidu.png");
                            return bitmap;
                        }
                        return null;
                    }
                })
                .subscribe(new Observer<Bitmap>(){
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Bitmap bitmap) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });







    }

    /**
     * 保存图片到本地，后面可以创建一个工具类
     * @param responseBody
     * @return
     */
    private boolean saveToSdcard(ResponseBody responseBody) {
        return false;
    }

    public void getIpAddress() {
        RetrofitFactory.getInstance()
                .getService()
                .getIP("http://ip-api.com/json")
                .compose(RxUtil.schedulers())
                .subscribe(new Consumer<IpAddressResponse>(){
                    @Override
                    public void accept(IpAddressResponse ipAddressResponse) throws Exception {
                        ToastUtil.show(MainActivity.this,ipAddressResponse.getQuery()+"测试");
                    }
                });
    }
}
